
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../utils/Settings.dart';
import '../formitems/cascader/LFormCascaderLogic.dart';
import '../formitems/cascader/LFormCascaderPage.dart';
import '../formitems/date/LFormDateLogic.dart';
import '../formitems/date/LFormDatePage.dart';
import '../formitems/datetime/LFormDatetimeLogic.dart';
import '../formitems/datetime/LFormDatetimePage.dart';
import '../formitems/fileup/LFormFileUpLogic.dart';
import '../formitems/fileup/LFormFileUpPage.dart';
import '../formitems/imageup/LFormImageUpLogic.dart';
import '../formitems/imageup/LFormImageUpPage.dart';
import '../formitems/input/LFormInputLogic.dart';
import '../formitems/input/LFormInputPage.dart';
import '../formitems/month/LFormMonthLogic.dart';
import '../formitems/month/LFormMonthPage.dart';
import '../formitems/multicascader/LFormMultiCascaderLogic.dart';
import '../formitems/multicascader/LFormMultiCascaderPage.dart';
import '../formitems/multiselect/LFormMultiSelectLogic.dart';
import '../formitems/multiselect/LFormMultiSelectPage.dart';
import '../formitems/select/LFormSelectLogic.dart';
import '../formitems/select/LFormSelectPage.dart';
import '../formitems/time/LFormTimeLogic.dart';
import '../formitems/time/LFormTimePage.dart';
import '../formitems/tree/LFormTreeLogic.dart';
import '../formitems/tree/LFormTreePage.dart';
import '../formitems/year/LFormYearLogic.dart';
import '../formitems/year/LFormYearPage.dart';
import 'LFormState.dart';

class LFormLogic extends GetxController{


  final state = LFormState();

  void init({required List formItems,required Map formData,String? formId,required String tag, Function? onChangeCallback}){
    state.formItems = formItems;
    state.formData = formData;
    state.formKey = GlobalKey<FormState>();
    state.tag = tag;
    state.onChangeCallback = onChangeCallback!;
  }

  ///生成表单中的组件
  List<Widget> createFormItes(){
    List<Widget> itemList = [];
    for(dynamic item in state.formItems){
      if(item["component"]=="input" && item["hide"]!=true) {
        itemList.add(createTextFormField(item));
      }else if(item["component"]=="select" && item["hide"]!=true){
        itemList.add(createSelectFormField(item)) ;
      }else if(item["component"]=="datetime" && item["hide"]!=true){
          itemList.add(createDateTimeFormField(item));
      }else if(item["component"]=="date" && item["hide"]!=true){
          itemList.add(createDateFormField(item));
      }else if(item["component"]=="year" && item["hide"]!=true){
          itemList.add(createYearFormField(item));
      }else if(item["component"]=="time" && item["hide"]!=true){
        itemList.add(createTimeFormField(item));
      }else if(item["component"]=="month" && item["hide"]!=true){
        itemList.add(createMonthFormField(item));
      }else if(item["component"]=="file" && item["hide"]!=true){
        itemList.add(createFUpFormField(item));
      }else if(item["component"]=="image" && item["hide"]!=true){
        itemList.add(createImageUpFormField(item));
      }else if(item["component"]=="cascader" && item["hide"]!=true){
        itemList.add(createCascaderFormField(item)) ;
      }else if(item["component"]=="multiselect" && item["hide"]!=true){
        itemList.add(createMultiSelectFormField(item));
      }else if(item["component"]=="multicascader"){
        itemList.add(createMultiCascaderFormField(item));
      }else if(item["component"]=="tree"){
        itemList.add(createTreeFormField(item));
      }
    }
    itemList.add(const SizedBox(height: 10,));
    return itemList.toList();
  }

  ///生成表单中的label
  Widget createFormLabel(Map item){
    String label = item["label"];
    if(label.length>8){
      label = "${label.substring(0,8)}..";
    }
    return Padding(
      padding: const EdgeInsets.only(left: 10),
      child: item["isShowLabel"]==true?
        (item["required"]==true?
          Row(children: [const Icon(Icons.star,color: Colors.red,size: 6,),Text(label,overflow: TextOverflow.ellipsis,style: const TextStyle(fontSize: 16,color: Colors.black54),)]):
          Row(children: [const SizedBox(width: 6,),Text(label,overflow: TextOverflow.ellipsis,style: const TextStyle(fontSize: 16,color: Colors.black54),)],)):
      null,
    );
  }

  ///生成输入框
  Widget createTextFormField(item){
    final LFormInputLogic formInputlogic = Get.put(LFormInputLogic(),tag: '${state.tag}#${item['field']}');
    formInputlogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormInputPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

   ///生成下拉
  Widget createSelectFormField(item){
    final LFormSelectLogic formSelectLogic = Get.put(LFormSelectLogic(),tag: '${state.tag}#${item['field']}');
    formSelectLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormSelectPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }
 ///多选生成下拉
  Widget createMultiSelectFormField(item){
    final LFormMultiSelectLogic formMultiSelectLogic = Get.put(LFormMultiSelectLogic(),tag: '${state.tag}#${item['field']}');
    formMultiSelectLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormMultiSelectPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }
  ///Cascader选择
  Widget createCascaderFormField(item){
    final LFormCascaderLogic lFormCascaderLogic = Get.put(LFormCascaderLogic(),tag: '${state.tag}#${item['field']}');
    lFormCascaderLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormCascaderPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///Cascader多选
  Widget createMultiCascaderFormField(item){
    final LFormMultiCascaderLogic lFormMultiCascaderLogic = Get.put(LFormMultiCascaderLogic(),tag: '${state.tag}#${item['field']}');
    lFormMultiCascaderLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormMultiCascaderPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///tree
  Widget createTreeFormField(item){
    final LFormTreeLogic formTreeLogic = Get.put(LFormTreeLogic(),tag: '${state.tag}#${item['field']}');
    formTreeLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormTreePage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///日期选择
  Widget createDateFormField(item){
    final LFormDateLogic lFormDateLogic = Get.put(LFormDateLogic(),tag: '${state.tag}#${item['field']}');
    lFormDateLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormDatePage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///时间选择
  Widget createTimeFormField(item){
    final LFormTimeLogic lFormTimeLogic = Get.put(LFormTimeLogic(),tag: '${state.tag}#${item['field']}');
    lFormTimeLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormTimePage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///年月日时分秒
  Widget createDateTimeFormField(item){
    final LFormDatetimeLogic formDatetimeLogic = Get.put(LFormDatetimeLogic(),tag:'${state.tag}#${item['field']}');
    formDatetimeLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormDatetimePage(tag:'${state.tag}#${item['field']}')
        )
      ],
    );
  }

  ///年
  Widget createYearFormField(item){
    final LFormYearLogic lFormYearLogic = Get.put(LFormYearLogic(),tag:'${state.tag}#${item['field']}');
    lFormYearLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormYearPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }
  ///月
  Widget createMonthFormField(item){
    final LFormMonthLogic lFormMonthLogic = Get.put(LFormMonthLogic(),tag:'${state.tag}#${item['field']}');
    lFormMonthLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormMonthPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }
  ///图片上传
  Widget createImageUpFormField(item){
    final LFormImageUpLogic lFormImageUpLogic = Get.put(LFormImageUpLogic(),tag:'${state.tag}#${item['field']}');
    lFormImageUpLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormImageUpPage(tag:'${state.tag}#${item['field']}')
        )
      ],
    );
  }

  ///文件上传
  Widget createFUpFormField(item){
    final LFormFileUpLogic lFormFileUpLogic = Get.put(LFormFileUpLogic(),tag:'${state.tag}#${item['field']}');
    lFormFileUpLogic.init(formItem: item, formData: state.formData,formTag: state.tag,formType: Settings.formType["表单"]);
    return Row(
      children: [
        Expanded(
          flex: 4,
          child: createFormLabel(item),
        ),
        Expanded(
            flex: 5,
            child:LFormFileUpPage(tag:'${state.tag}#${item['field']}',)
        )
      ],
    );
  }

  ///获取FORM的ID
  String getFormTag(){
    return state.tag;
  }

  ///获取FORM的KEY
  GlobalKey getFormKey(){
    return state.formKey;
  }

  ///获取表单数据
  Map getFormData(){
    return state.formData;
  }

  onChange(changeItem){
    // print(changeItem);
    if(state.onChangeCallback!=null){
      state.onChangeCallback(changeItem);
    }

  }

  void setFormData(formData){
    state.formData = formData;
    update();
  }
}